package com.visikard.business;

import org.json.JSONArray;

import android.util.Log;

import com.visikard.application.Constant;
import com.visikard.item.ItemRatingReview;
import com.visikard.net.VKregisterNetworkException;
import com.visikard.request.AudienceRequest;
import com.visikard.response.BaseResponse;
import com.visikard.response.GetAudienceFriendsResponse;
import com.visikard.response.GetAudienceHistoryResponse;
import com.visikard.response.GetListUserRatingResponse;
import com.visikard.response.GetRatingReviewResponse;
import com.visikard.response.SendAudienceToWalletResponse;
import com.visikard.utility.Utils;

public class AudienceBUS {

	public GetAudienceHistoryResponse getAudienceHistory(Long fkUser, long idCampaignSpecail, int pageIndex,
			int pageSize) {
		AudienceRequest ar = new AudienceRequest();
		GetAudienceHistoryResponse result = new GetAudienceHistoryResponse();
		try {
			String response = ar.getAudienceHistory(fkUser, idCampaignSpecail, pageIndex, pageSize);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public GetAudienceFriendsResponse getAudienceFriends(Long fkUser, String sessionID) {
		AudienceRequest ar = new AudienceRequest();
		GetAudienceFriendsResponse result = new GetAudienceFriendsResponse();
		try {
			String response = ar.getAudienceFriends(fkUser, sessionID);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public SendAudienceToWalletResponse sendAudienceToWallet(long uid, long idCampaignSpecail, String listfkUser,
			String listfkKard, int isBroadcast) {
		AudienceRequest ar = new AudienceRequest();
		SendAudienceToWalletResponse result = new SendAudienceToWalletResponse();
		try {
			String response = ar.sendAudienceToWallet(uid, idCampaignSpecail, listfkUser, listfkKard, isBroadcast);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public GetRatingReviewResponse getRatingReview(Long fkUser, String idCampaignSpecail) {
		AudienceRequest ar = new AudienceRequest();
		GetRatingReviewResponse result = new GetRatingReviewResponse();
		try {
			String response = ar.getRatingReview(fkUser, idCampaignSpecail);

			if (response != null && !response.isEmpty() && Utils.isValidJsonArray(response)) {
				// response OK
				JSONArray jsArr = new JSONArray(response);
				result.setItemRatingReview(new ItemRatingReview(jsArr.getJSONObject(0)));
			}
			else {
				Log.e(Constant.LOG_TAG, "forgotPass: " + "Invalid response!!!");
				result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
				result.setErrorMsg("Invalid response!!!");
			}
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public GetListUserRatingResponse getListUserRating(String idCampaignSpecail, int pageIndex, int pageSize) {
		AudienceRequest ar = new AudienceRequest();
		GetListUserRatingResponse result = new GetListUserRatingResponse();
		try {
			String response = ar.getListUserRating(idCampaignSpecail, pageIndex, pageSize);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "getAllCoupons: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

}
